//
//  ViewController.swift
//  QQChat_Swift3.0_1
//
//  Created by 李明禄 on 2016/11/29.
//  Copyright © 2016年 SocererGroup. All rights reserved.
//

import UIKit
import UserNotifications


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UNUserNotificationCenterDelegate{
    
    @IBOutlet weak var talkView: UIView!
    
    @IBOutlet weak var input: UITextField!

    
    var tableView: UITableView!
    let screenBounds = UIScreen.main.bounds
    
    var type = 0 ;
    
    let identifier = "message"
    
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
   //发送消息
        let inputContent = self.input.text!
        //将内容添加到plist文件中
        sendMessage(type: 0, messge: inputContent)
        
//        autoAnswer()
        
        print(inputContent)
        return true
    }
    
    
    @IBAction func otherSendInfo(_ sender: UIButton) {
        
        sendMessage(type: 1, messge: input.text!)
        
    }
    
    override func prefersHomeIndicatorAutoHidden() -> Bool {
        return true
    }
    
    func autoAnswer() {
        if self.type == 3 {
            return
        }
        sendMessage(type: 1, messge: "我是机器人回答的")
    }
    
    func sendMessage(type: NSNumber, messge: String) {
        
        if (trimedWhiteSpace(message: messge)) {
            //初始化模型
            let message: Message = Message.init()
            message.type = type
            message.text = messge
            
            
            let time = Date.init()
            let dateFormat = DateFormatter.init()
            dateFormat.dateFormat = "今天 HH:mm"
            let timeString = dateFormat.string(from: time)
            message.time = timeString
            
            //设置frame模型
            let frameModel = LSMessageFrame.init()
            frameModel.message = message
            
            self.messageFrames.append(frameModel)
            
            self.tableView.reloadData()
            
            let indexPath = IndexPath.init(row: self.messageFrames.count - 1, section: 0)
            
            
            self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: false)
            self.input.text = ""
        }
        
    }
    
    /// 把消息的内容内去掉空格
    ///
    /// - Parameter message: 发送的消息
    /// - Returns:
///          false: 无内容
    ///          true:有内容
    fileprivate func trimedWhiteSpace(message: String) -> Bool {
        
        let set = CharacterSet.whitespacesAndNewlines
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        let trimdString = message.trimmingCharacters(in: set)
        
        if trimdString.isEmpty || trimdString == nil {
//            self.type = 3
            return false
        } else
        {
           return true
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let tableView = UITableView.init(frame: screenBounds)
//        
        tableView.delegate = self
        tableView.dataSource = self
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never
        } else {
            // Fallback on earlier versions
        }
        
        self.tableView = tableView
        
        self.tableView.backgroundColor = UIColor.gray
        self.view.addSubview(tableView)
        
        //注册一个cell
        self.tableView.register(LSMessageCell.self, forCellReuseIdentifier: identifier)
        
        //去掉分隔线
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
        //设置tableViewInsets
        let insets = UIEdgeInsetsMake(44, 0, 50, 0)
        self.tableView.contentInset = insets
        
        //添加聊天输入view
        self.view.addSubview(talkView!)
        
        let center = NotificationCenter.default
        
        center.addObserver(self, selector: #selector(self.keyboardWillChangeFrame(note:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
        
        
        
        self.input.delegate = self
        self.input.resignFirstResponder()
        

        //通过添加手势点击空白位置时键盘隐藏
        let tapGuesture = UITapGestureRecognizer.init(target: self, action: #selector(tapView(tap:)))
        
        self.view.addGestureRecognizer(tapGuesture)
  
    }
    
    func tapView(tap: UITapGestureRecognizer) {
        
        self.view.endEditing(true)
    }
    
    func keyboardWillChangeFrame(note: Notification) {
        let keyboardDuration = note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
        let keyboardFrame = note.userInfo![UIKeyboardFrameEndUserInfoKey] as! CGRect
        //取得键盘移动的最终Y值
        let transformY = keyboardFrame.origin.y - self.view.bounds.height
        
                
//        设置View的约束
        self.view.transform = CGAffineTransform.init(translationX: 0, y: transformY)
    }
    
    
    
    
    
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
   
    // MARK: --实现tableView的代理方法--
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.messageFrames!.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let messageFrame = self.messageFrames?[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! LSMessageCell
        
        cell.cellFrame = messageFrame
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let messageFrame = self.messageFrames![indexPath.row]
        return messageFrame.cellHeight!
    }
    
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    lazy var messageFrames: [LSMessageFrame]! = {
        let path = Bundle.main.path(forResource: "send.plist", ofType: nil)
        
        let messageArray = NSArray(contentsOfFile: path!)
        
        let messages = Message.dict2Model(list: messageArray! as! [[String : AnyObject]])
        
        var messageFramees: [LSMessageFrame]! = []
        for message: Message in messages {
            let messageFrame = LSMessageFrame.init()
            var lastFrame: LSMessageFrame! = nil
            print("=====" + "\(message)")
            if messageFramees!.count != 0  {
                lastFrame = messageFramees!.last!
                if lastFrame.message.time == message.time {
                    messageFrame.isTimeHide = true
                }
            
            }
            
            
//            print("____" +)
            messageFrame.message = message
            messageFramees!.append(messageFrame)
        }
        print("**************")
//        print(messageFrames!.count)
        return messageFramees
    }()
    
    
    
   


}

